<!DOCTYPE html>

<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">

<title>GETSTART - TJPlurker API Documentation</title>

<link type="text/css" media="screen" href="./rdoc.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "./";
</script>

<script type="text/javascript" charset="utf-8" src="./js/jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="./js/navigation.js"></script>
<script type="text/javascript" charset="utf-8" src="./js/search_index.js"></script>
<script type="text/javascript" charset="utf-8" src="./js/search.js"></script>
<script type="text/javascript" charset="utf-8" src="./js/searcher.js"></script>
<script type="text/javascript" charset="utf-8" src="./js/darkfish.js"></script>


<body class="file">
<nav id="metadata">
  <nav id="home-section" class="section">
  <h3 class="section-header">
    <a href="./index.html">Home</a>
    <a href="./table_of_contents.html#classes">Classes</a>
    <a href="./table_of_contents.html#methods">Methods</a>
  </h3>
</nav>


  <nav id="search-section" class="section project-section" class="initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <h3 class="section-header">
      <input type="text" name="search" placeholder="Search" id="search-field"
             title="Type to search, Up and Down to navigate, Enter to load">
    </h3>
  </form>

  <ul id="search-results" class="initially-hidden"></ul>
</nav>


  <div id="project-metadata">
    <nav id="fileindex-section" class="section project-section">
  <h3 class="section-header">Pages</h3>

  <ul>
  
    <li class="file"><a href="./GETSTART.html">GETSTART</a>
  
    <li class="file"><a href="./MAKEROBOT.html">MAKEROBOT</a>
  
    <li class="file"><a href="./README.html">README</a>
  
  </ul>
</nav>

    <nav id="classindex-section" class="section project-section">
  <h3 class="section-header">Class and Module Index</h3>

  <ul class="link-list">
  
    <li><a href="./Legacy.html">Legacy</a>
  
    <li><a href="./Legacy/TJPlurker.html">Legacy::TJPlurker</a>
  
    <li><a href="./Legacy/TJPlurker/NotificationData.html">Legacy::TJPlurker::NotificationData</a>
  
    <li><a href="./Legacy/TJPlurker/Robot.html">Legacy::TJPlurker::Robot</a>
  
    <li><a href="./Legacy/TJPlurker/Service.html">Legacy::TJPlurker::Service</a>
  
    <li><a href="./Legacy/TJPlurker/ThreadService.html">Legacy::TJPlurker::ThreadService</a>
  
    <li><a href="./TJP.html">TJP</a>
  
    <li><a href="./TJP/NotificationData.html">TJP::NotificationData</a>
  
    <li><a href="./TJP/Robot.html">TJP::Robot</a>
  
    <li><a href="./TJP/Service.html">TJP::Service</a>
  
    <li><a href="./TJP/TJPlurker.html">TJP::TJPlurker</a>
  
    <li><a href="./TJP/ThreadService.html">TJP::ThreadService</a>
  
  </ul>
</nav>

  </div>
</nav>

<div id="documentation" class="description">
  
<h1 id="label-%E8%B1%88%E4%B8%8D%E8%B5%B0">豈不走</h1>

<h2 id="label-Dependence">Dependence</h2>
<ul><li>
<p><a href="http://www.ruby-lang.org">Ruby</a></p>
</li><li>
<p><a href="https://rubygems.org/gems/json">json</a></p>
</li><li>
<p><a href="https://rubygems.org/gems/oauth">oauth</a></p>
</li></ul>

<h2 id="label-%E5%AE%89%E8%A3%9D">安裝</h2>

<p>你有以下方式取得TJPlurker：</p>
<ul><li>
<p><a href="https://rubygems.org/gems/tjplurker">RubyGems</a>（建議）</p>

<pre>$ gem install tjplurker</pre>
</li><li>
<p><a href="http://code.google.com/p/tjplurker/source/checkout">Mercurial</a></p>

<pre>$ hg clone https://tonytonyjan@code.google.com/p/tjplurker/ tjplurker</pre>
</li></ul>

<h3 id="label-%E5%9F%B7%E8%A1%8C">執行</h3>

<p>由於原始碼都是utf8編碼，如果內有Big5中文字，可能造成無法編譯，要加上-Ku參數</p>

<pre>ruby -Ku your_program.rb</pre>

<h2 id="label-Authorization">Authorization</h2>

<p>TJPlurker 的兩個核心物件， <a href="TJP/TJPlurker.html">TJP::TJPlurker</a> 和 <a
href="TJP/Robot.html">TJP::Robot</a> ，的建構需要有 <a
href="http://oauth.net/core/1.0a">Oauth 1.0</a> 的背景知識，建議你參考 <a
href="http://www.plurk.com/API">Plurk API</a> 的流程圖。在使用 TJPlurker 之前你必需要先到
<a href="http://www.plurk.com/PlurkApp/register">此頁</a> 申請你的APP以取得 consumer
key 和 consumer secret。</p>

<h3 id="label-TJP%3A%3ATJPlurker"><a href="TJP/TJPlurker.html">TJP::TJPlurker</a></h3>

<pre>new(consumer_key, consumer_secret, access_token=nil, access_token_screte=nil)</pre>

<p>Plurk API2.0的呼叫不盡然用到 Access Token 和 Access Token Screte （例如 <a
href="http://www.plurk.com/API#/APP/Profile/getPublicProfile">/APP/Profile/getPublicProfile</a>），所以可以視情況留空。</p>

<p>如果你尚未有 access token 則可以用下列方法取得：</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&quot;tjplurker&quot;</span>
<span class="ruby-identifier">tjp</span> = <span class="ruby-constant">TJP</span><span class="ruby-operator">::</span><span class="ruby-constant">TJPlurker</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&quot;tPncMPx4HhTx&quot;</span>, <span class="ruby-string">&quot;sck0hH0nAD01E4zwZguJzlqhjtEYvpc0&quot;</span>) 
<span class="ruby-identifier">tjp</span>.<span class="ruby-identifier">authorize</span>
</pre>

<p>這斷成是會產生一串網址，你必須透過該網址取得認證碼，並將認證碼輸入其中，輸出畫面：</p>

<pre>Authorize URL: http://www.plurk.com/OAuth/authorize?oauth_token=OAUTH_TOKEN
Please Enter your verification code: VERIFICATION_CODE
INFO [2011-07-15 18:38:51 +0800] in TJP::TJPlurker -- Access Token: ACCESS_TOKEN, Access Token Secret: ACCESS_TOKEN_SECRET</pre>

<h3 id="label-TJP%3A%3ARobot"><a href="TJP/Robot.html">TJP::Robot</a></h3>

<pre>new(tjplurker)
new(key, screte)
new(key, secret, access_token, access_token_screte)*</pre>

<p>與TJP::TJPlurker的初始方式雷同，但可將已初始化完畢的TJP::TJPlurker物件當參數</p>

<h2 id="label-%E4%BD%BF%E7%94%A8TJPlurker">使用TJPlurker</h2>

<h3 id="label-%E4%BD%BF%E7%94%A8Plurk+API+2.0">使用Plurk API 2.0</h3>

<p>TJP::TJPlurker提供Plurk API的包裹，並針對不方便的API實做出更容易使用的介面，想知道更多關於Plurk API的使用，請閱讀
<a href="http://www.plurk.com/API">Plurk API</a>。</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'tjplurker'</span>
<span class="ruby-identifier">include</span> <span class="ruby-constant">TJP</span>
<span class="ruby-identifier">tjp</span> = <span class="ruby-constant">TJPlurker</span>.<span class="ruby-identifier">new</span>(<span class="ruby-constant">CONSUMER_KEY</span>, <span class="ruby-constant">CONSUMER_SECRET</span>, <span class="ruby-constant">ACCESS_TOKEN</span>, <span class="ruby-constant">ACCESS_TOKEN_SECRET</span>)
<span class="ruby-identifier">tjp</span>.<span class="ruby-identifier">plurk_add</span>(<span class="ruby-string">&quot;Hello TJPlurker&quot;</span>)
</pre>

<p>在 <a href="http://www.plurk.com/API">Plurk API</a>
中你會發現有很多的參數需要用到user_id，但是我們通常會記得的是使用者的帳號，而不是id，所以TJP裡面提供了get_uid方便使用者做轉換。如下兩行是一樣的意思：</p>

<pre>tjp.get_public_profile(get_uid(&quot;tonytonyjan&quot;))
tjp.get_public_profile(&quot;5874158&quot;)</pre>

<p>TJP::TJPlurker還在開發中，未必有作到全部的包裹，你可以透過參考 <a
href="http://www.plurk.com/API">Plurk API</a>，並直接呼叫，例如：</p>

<pre class="ruby"><span class="ruby-identifier">tjp</span>.<span class="ruby-identifier">api</span>(<span class="ruby-string">&quot;/APP/Responses/responseAdd&quot;</span>, {:<span class="ruby-identifier">plurk_id=</span><span class="ruby-operator">&gt;</span><span class="ruby-constant">PLURK_ID</span>, :<span class="ruby-identifier">content=</span><span class="ruby-operator">&gt;</span><span class="ruby-string">&quot;Hello&quot;</span>, :<span class="ruby-identifier">qualifier=</span><span class="ruby-operator">&gt;</span><span class="ruby-string">&quot;:&quot;</span>})
</pre>

<h3 id="label-%E8%A3%BD%E4%BD%9C%E5%99%97%E6%B5%AA%E6%A9%9F%E5%99%A8%E4%BA%BA">製作噗浪機器人</h3>

<p>示範一隻應聲蟲機器人，他會像鸚鵡一樣重複回應所有已聽到的訊息，除此會在每分鐘自動答應所有的好友邀請：</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'tjplurker'</span>
<span class="ruby-identifier">include</span> <span class="ruby-constant">TJP</span>
<span class="ruby-identifier">robot</span> = <span class="ruby-constant">Robot</span>.<span class="ruby-identifier">new</span>(<span class="ruby-constant">CONSUMER_KEY</span>, <span class="ruby-constant">CONSUMER_SECRETE</span>, <span class="ruby-constant">ACCESS_TOKEN</span>, <span class="ruby-constant">ACCESS_TOKEN_SECRET</span>)
<span class="ruby-identifier">service_echo</span> = <span class="ruby-constant">Service</span>.<span class="ruby-identifier">new</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">tjp</span>, <span class="ruby-identifier">data</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">tjp</span>.<span class="ruby-identifier">response_add</span>(<span class="ruby-identifier">data</span>.<span class="ruby-identifier">plurk_id</span>, <span class="ruby-identifier">data</span>.<span class="ruby-identifier">content</span>)
}
<span class="ruby-identifier">robot</span>.<span class="ruby-identifier">add_service</span>(<span class="ruby-identifier">service_echo</span>)
<span class="ruby-identifier">robot</span>.<span class="ruby-identifier">start</span>
</pre>

<h4 id="label-%E5%AE%9A%E7%BE%A9Service">定義Service</h4>

<p>這裡是我們製作Echo機器人的程式碼所在：</p>

<pre class="ruby"><span class="ruby-identifier">service_echo</span> = <span class="ruby-constant">TJPlurker</span><span class="ruby-operator">::</span><span class="ruby-constant">Service</span>.<span class="ruby-identifier">new</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">tjp</span>, <span class="ruby-identifier">data</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">tjp</span>.<span class="ruby-identifier">response_add</span>(<span class="ruby-identifier">data</span>.<span class="ruby-identifier">plurk_id</span>, <span class="ruby-identifier">data</span>.<span class="ruby-identifier">content</span>)}}}
}
</pre>
<dl class="rdoc-list note-list"><dt>tjp
<dd>
<p>是TJP::TJPlurker物件，可以讓你執行發噗、回噗、加朋友等等各式各樣的操作</p>
</dd><dt>data
<dd>
<p>是TJP::NotificationData，指河道上監聽到的每則新訊息，含時間、作者、內容等等資訊。</p>
</dd></dl>

<p>藉由tjp和data的搭配可以做出任何回應式的機器人。而在block裡面看到response_add的參數分別是目標噗的ID以及回應的內容，在此可以看到tjp只是把來自data中的content原封不動的回覆回去。</p>

<p>你可以在TJP::NotificationData找到更多data物件的詳細資料</p>

<h3 id="label-%E8%A8%BB%E5%86%8A%E6%96%B0%E6%9C%8D%E5%8B%99">註冊新服務</h3>

<pre>robot.add_service(service_echo)</pre>

<p>將你剛剛的服務註冊到機器人。機器人裡面有一個「服務庫」，存放著所有被你加入的TJP::Service物件。</p>

<h3 id="label-%E5%95%9F%E5%8B%95%E6%A9%9F%E5%99%A8%E4%BA%BA">啟動機器人</h3>

<pre>robot.start</pre>

<p>裡面有個無窮迴圈用以監聽河道，一聽到新訊息就執行“服務庫”中的服務，其功能則是當初你在創建服務時{}中的程式碼。</p>

</div>



<footer id="validator-badges">
  <p><a href="http://validator.w3.org/check/referer">[Validate]</a>
  <p>Generated by <a href="https://github.com/rdoc/rdoc">RDoc</a> 3.12.
  <p>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish Rdoc Generator</a> 3.
</footer>

